package com.googlecode.sparda.commons.swing;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JDialog;


/**
 * A complementary Toolkit to the {@link Toolkit}.
 * @author Fabio Grucci (<i>alias</i> <b>Phoenix</b>)
 * @since 1.0
 * @version 1.0
 * @see Toolkit
 */
public final class PhoenixToolkit {
	
	/**
	 * The default java {@link Toolkit}.
	 */
	private final Toolkit toolkit;

	/**
	 * Build the toolkit.
	 */
	public PhoenixToolkit() {
		this.toolkit = Toolkit.getDefaultToolkit();
	}
	
	/**
	 * Methods that return the medium point of the current screen.
	 * @return The medium point of the screen
	 */
	public Point getMediumPoint() {
		final Dimension screenSize = this.toolkit.getScreenSize();
		return new Point(screenSize.width/2, screenSize.height/2);
	}
	
	/**
	 * Methods that return the medium point to set in the center a window of this dimension.
	 * @param width The width of the object to center
	 * @param height The height of the object to center
	 * @return The point to center the object
	 */
	public Point getMediumPoint(int width, int height) {
		final Dimension screenSize = this.toolkit.getScreenSize();
		return new Point(screenSize.width/2-width/2, screenSize.height/2-height/2);
	}
	
	/**
	 * Method that return the medium point to set in the center an object of a certain {@link Dimension}.
	 * @param dimension The dimension of the object
	 * @return The point where center the object
	 */
	public Point getMediumPoint(Dimension dimension) {
		final Dimension screenSize = this.toolkit.getScreenSize();
		return new Point(screenSize.width/2-dimension.width/2, screenSize.height/2-dimension.height/2);
	}
	
	/**
	 * Method that set in the center of the screen a {@link Component} .
	 * @param component The {@link Component} to center
	 */
	public void centre(Component component) {
		component.setLocation(this.getMediumPoint(component.getSize()));
	}
	
	/**
	 * Methods that set in the center a component inside another component.
	 * Typically this method is used to set in the center a component (e.g. {@link Dialog} or {@link JDialog}), 
	 * at the center of the window that opened the component itself.
	 * @param pivot The pivot component
	 * @param toCenter The component to center
	 */
	public static final void centre(Component pivot, Component toCenter) {
		final Dimension d1 = pivot.getSize();
		final Dimension d2 = toCenter.getSize();
		final Point p1 = pivot.getLocation();
		toCenter.setLocation(p1.x + d1.width/2 - d2.width/2, p1.y + d1.height/2 -d2.height/2);
	}
	
}
